package com.container.util;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

import io.netty.util.concurrent.FastThreadLocalThread;

public class MyThreadFactory implements ThreadFactory {
	private final String prefix;
    private final boolean daemon;
    private final int priority;
    private final ThreadGroup group;
    private final AtomicInteger number = new AtomicInteger(1);

    public MyThreadFactory(String prefix, boolean daemon, int priority) {
        SecurityManager s = System.getSecurityManager();
        this.group = (s == null) ? Thread.currentThread().getThreadGroup() : s.getThreadGroup();
        this.prefix = prefix;
        this.daemon = daemon;
        this.priority = priority;
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread t = newThread(r, prefix + number.incrementAndGet());
        try {
            if (t.isDaemon() != daemon) {
                t.setDaemon(daemon);
            }

            if (t.getPriority() != priority) {
                t.setPriority(priority);
            }
        } catch (Exception ignored) {
            
        }
        return t;
    }

    protected Thread newThread(Runnable r, String name) {
        return new FastThreadLocalThread(group, r, name);
    }
}